import { Button, Form, Input } from "antd";
import { login } from "../../api/flask";
import useRouter from "../../hooks/useRouter";

type FieldType = {
    email: string;
    password: string;
}

const Index = () => {
    const [form] = Form.useForm<FieldType>();
    const { navigate } = useRouter()

    const onFinish = (values: FieldType) => {
        console.log('Success:', values);
        login(values).then((res) => {
            console.log(res)
            navigate('/index')
        })
    };

    const handleGo = () => {
        navigate('/register')
    }

    return (
        <div>
            <div>还没有账号，去注册<Button onClick={handleGo}>注册</Button></div>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                style={{ maxWidth: 600 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item<FieldType>
                    label="邮箱"
                    name="email"
                    required
                >
                    <Input />
                </Form.Item>


                <Form.Item<FieldType>
                    label="Password"
                    name="password"
                    required
                >
                    <Input.Password />
                </Form.Item>


                <Form.Item label={null}>
                    <Button type="primary" htmlType="submit">
                        Submit
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Index